package org.example.quartz.controller;


import lombok.extern.slf4j.Slf4j;
import org.example.quartz.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController("glControllerSlave1")
@RequestMapping(path = "/GSYY/GLController")
public class GLController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Value(value = "${spring.datasource.databaseType}")
    private String databaseType;
    @Value(value ="${spring.datasource.GSYY}")
    private String GSYY;

    /**
     * 列表查询
     *
     * @param param
     * @return
     */
    @PostMapping("/getList")
    public Result getList(@RequestBody Map<String, Object> param) {
        try {
            String condition = "";
            //拼接条件
            if (!ObjectUtils.isEmpty(param.get("beginTime"))) {
                condition += " and ACCDOCDATE >= '"+param.get("beginTime")+" 00:00:00'";
            }
            if(!ObjectUtils.isEmpty(param.get("endTime"))){
                condition += " and ACCDOCDATE <= '"+param.get("endTime")+" 23:59:59'";
            }
            if(!ObjectUtils.isEmpty(param.get("ACCDOCCODE"))){
                condition += " and ACCDOCCODE = '记"+param.get("ACCDOCCODE")+"'";
            }
            if(!ObjectUtils.isEmpty(param.get("SUMMARY"))){
                condition += " and SUMMARY like '%"+param.get("SUMMARY")+"%'";
            }
            if(!ObjectUtils.isEmpty(param.get("PUSHSTATUS"))){
                if(param.get("PUSHSTATUS").toString().equals("成功")){
                    param.put("GSSTATUS","1");
                }else if(param.get("PUSHSTATUS").toString().equals("失败")){
                    param.put("GSSTATUS","2");
                }
               // condition += " and PUSHSTATUS = '"+param.get("PUSHSTATUS")+"'";
                condition += " and GSSTATUS = '"+param.get("GSSTATUS")+"'";
            }
            if(!ObjectUtils.isEmpty(param.get("REFERENCEID"))){
                if(param.get("REFERENCEID").equals("未回写")){
                    condition += " and (REFERENCEID IS NULL or REFERENCEID='')";
                }else{
                    condition += " and (REFERENCEID IS NOT NULL and REFERENCEID !='')";
                }
            }
            Integer count = jdbcTemplate.queryForObject("select count(*) from "+GSYY+".gl_detail where 1 = 1 " + condition, Integer.class);
            if ("oracle".equals(databaseType)) {
                List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from "+GSYY+".gl_detail where 1=1 " + condition + "order by TS desc OFFSET ((" + Integer.parseInt(param.get("pageNum").toString()) + " - 1 ) *" + Integer.parseInt(param.get("pageSize").toString()) + ") rows fetch next " + Integer.parseInt(param.get("pageSize").toString()) + " rows only");
                return Result.success(mapList, count);
            }else if("dm".equals(databaseType)){
                String sql = "select * from (select t.*,ROWNUM rn from " + GSYY + ".gl_detail t where 1 = 1 " + condition + ") where rn between " + (Integer.parseInt(param.get("pageNum").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()) + " and " + (Integer.parseInt(param.get("pageNum").toString())) * Integer.parseInt(param.get("pageSize").toString());
                List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
                return Result.success(mapList, count);
            } else {
                List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from "+GSYY+".gl_detail where 1=1 " + condition + " limit " + Integer.parseInt(param.get("pageSize").toString()) + " OFFSET " + (Integer.parseInt(param.get("pageNum").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
                return Result.success(mapList, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }




}
